<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="退货时间" prop="refuseTime">
        <el-date-picker clearable v-model="queryParams.refuseTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择退货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户编号" prop="costumId">
        <el-input v-model="queryParams.costumId" placeholder="请输入客户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="不合格数量" prop="unqualifiedCount">
        <el-input v-model="queryParams.unqualifiedCount" placeholder="请输入不合格数量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="措施" prop="measure">
        <el-input v-model="queryParams.measure" placeholder="请输入措施" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否索赔" prop="isClaim">
        <el-select v-model="queryParams.isClaim" placeholder="请选择是否索赔" clearable style="width: 108px;">
          <el-option v-for="dict in quality_is_claim" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="索赔价格" prop="claimPrice">
        <el-input v-model="queryParams.claimPrice" placeholder="请输入索赔价格" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['quality:exLoss:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['quality:exLoss:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['quality:exLoss:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['quality:exLoss:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exLossList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="exLossId" /> -->
      <el-table-column label="退货时间" align="center" prop="refuseTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.refuseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户编号" align="center" prop="costumId" />
      <el-table-column label="物料编号" align="center" prop="materialId" />
      <el-table-column label="不合格数量" align="center" prop="unqualifiedCount" />
      <el-table-column label="措施" align="center" prop="measure" />
      <el-table-column label="是否索赔" align="center" prop="isClaim">
        <template #default="scope">
          <dict-tag :options="quality_is_claim" :value="scope.row.isClaim" />
        </template>
      </el-table-column>
      <el-table-column label="索赔价格" align="center" prop="claimPrice" />
      <el-table-column label="退款损失费用" align="center" prop="refuseLossPrice" />
      <el-table-column label="维修费用" align="center" prop="fixPrice" />
      <el-table-column label="折扣损失费用" align="center" prop="discountLossPrice" />
      <el-table-column label="诉讼费用" align="center" prop="litigationPrice" />
      <el-table-column label="供应商编号" align="center" prop="supplierId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:exLoss:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['quality:exLoss:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改外部损失管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="exLossRef" :model="form" label-width="108px" :rules="rule">
        <el-form-item label="退货时间" prop="refuseTime">
          <el-date-picker clearable v-model="form.refuseTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择退货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户编号" prop="costumId">
          <el-select v-model="form.costumId" placeholder="客户编号" style="width: 240px">
            <el-option v-for="item in customList" :key="item.customId" :label="item.customId" :value="item.customId" />
          </el-select>
        </el-form-item>
        <el-form-item label="物料编号" prop="materialId">
          <el-select v-model="form.materialId" placeholder="物料编号" style="width: 240px">
            <el-option v-for="item in materialList" :key="item.materialId" :label="item.materialId"
              :value="item.materialId" />
          </el-select>
        </el-form-item>
        <el-form-item label="不合格数量" prop="unqualifiedCount">
          <el-input v-model="form.unqualifiedCount" placeholder="请输入不合格数量" />
        </el-form-item>
        <el-form-item label="措施" prop="measure">
          <el-input v-model="form.measure" placeholder="请输入措施" />
        </el-form-item>
        <el-form-item label="是否索赔" prop="isClaim">
          <el-select v-model="form.isClaim" placeholder="请选择是否索赔">
            <el-option v-for="dict in quality_is_claim" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="索赔价格" prop="claimPrice">
          <el-input v-model="form.claimPrice" placeholder="请输入索赔价格" />
        </el-form-item>
        <el-form-item label="退款损失费用" prop="refuseLossPrice">
          <el-input v-model="form.refuseLossPrice" placeholder="请输入退款损失费用" />
        </el-form-item>
        <el-form-item label="维修费用" prop="fixPrice">
          <el-input v-model="form.fixPrice" placeholder="请输入维修费用" />
        </el-form-item>
        <el-form-item label="折扣损失费用" prop="discountLossPrice">
          <el-input v-model="form.discountLossPrice" placeholder="请输入折扣损失费用" />
        </el-form-item>
        <el-form-item label="诉讼费用" prop="litigationPrice">
          <el-input v-model="form.litigationPrice" placeholder="请输入诉讼费用" />
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="供应商编号" style="width: 240px">
            <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierId"
              :value="item.supplierId" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ExLoss">
import { listExLoss, getExLoss, delExLoss, addExLoss, updateExLoss } from "@/api/quality/exLoss";
import { number } from "echarts";
import { reactive } from "vue";
import { listAllCustom } from "@/api/quality/custom";
import { listAllSupplier } from "@/api/quality/supplier";
import { listAllMaterial } from "@/api/quality/material";
const { proxy } = getCurrentInstance();
const { quality_is_claim } = proxy.useDict('quality_is_claim');
const customList = ref([])
const supplierList = ref([])
const materialList = ref([])
const exLossList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const rule = reactive({
  costumId: [{ required: true, message: '不能为空', trigger: 'blur' }, { type: 'number', message: '需要整数', trigger: 'blur' }],
  materialId: [{ required: true, message: '不能为空', trigger: 'blur' }, { type: 'number', message: '需要整数', trigger: 'blur' }],
  supplierId: [{ required: true, message: '不能为空', trigger: 'blur' }, { type: 'number', message: '需要整数', trigger: 'blur' }],
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    refuseTime: null,
    costumId: null,
    materialId: null,
    imageNumber: null,
    unqualifiedCount: null,
    measure: null,
    isClaim: null,
    claimPrice: null,
    supplierId: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询外部损失管理列表 */
function getList() {
  loading.value = true;
  listExLoss(queryParams.value).then(response => {
    exLossList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getCustomList() {
  listAllCustom().then(res => {
    customList.value = res.data;
  })
}

function getSupplierList() {
  listAllSupplier().then(res => {
    supplierList.value = res.data;
  })
}

function getMaterialList() {
  listAllMaterial().then(res => {
    materialList.value = res.data;
  })
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    exLossId: null,
    refuseTime: null,
    costumId: null,
    materialId: null,
    imageNumber: null,
    unqualifiedCount: null,
    measure: null,
    isClaim: null,
    claimPrice: null,
    refuseLossPrice: null,
    fixPrice: null,
    discountLossPrice: null,
    litigationPrice: null,
    createTime: null,
    createBy: null,
    supplierId: null,
  };
  proxy.resetForm("exLossRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.exLossId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加外部损失管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _exLossId = row.exLossId || ids.value
  getExLoss(_exLossId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改外部损失管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["exLossRef"].validate(valid => {
    if (valid) {
      if (form.value.exLossId != null) {
        updateExLoss(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addExLoss(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _exLossIds = row.exLossId || ids.value;
  proxy.$modal.confirm('是否确认删除外部损失管理编号为"' + _exLossIds + '"的数据项？').then(function () {
    return delExLoss(_exLossIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('quality/exLoss/export', {
    ...queryParams.value
  }, `exLoss_${new Date().getTime()}.xlsx`)
}
getCustomList();
getSupplierList();
getMaterialList();
getList();
</script>
